<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="8">
            <a-form-model-item label="工单号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="orderNo">
              <a-input v-model="model.orderNo" placeholder="请输入工单号"  disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="工单类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="orderType">
              <j-dict-select-tag type="list" v-model="model.orderType" dictCode="pp_order_type" placeholder="请选择工单类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="投放日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="putDate">
              <j-date placeholder="请选择投放日期" v-model="model.putDate"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="产品" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialDesc">
              <j-popup
                v-model="model.materialDesc"
                field="materialDesc"
                org-fields="material_id,material_code,material_desc,material_unit_code"
                dest-fields="materialId,materialNo,materialDesc,unit"
                code="mdm_popup_bom_material"
                @input="materialPopupCallback" placeholder="请选择产品"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="unit">
              <j-dict-select-tag type="list" v-model="model.unit" dictCode="mdm_unit,unit_name,unit_code" placeholder="请选择单位" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="工单数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="orderQty">
              <a-input-number v-model="model.orderQty" placeholder="请输入工单数量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="项目" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectId">
              <j-search-select-tag v-model="model.projectId" dict="crm_project,project_name,id"  />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="客户" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="customerId">
              <j-search-select-tag v-model="model.customerId" dict="crm_customer,cust_full_name,id"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="工单来源" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sourceType">
              <j-dict-select-tag type="list" v-model="model.sourceType" dictCode="pp_order_source" placeholder="请选择工单来源" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="计划开工期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="planStartDate">
              <j-date placeholder="请选择计划开工期" v-model="model.planStartDate"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="计划完工期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="planEndDate">
              <j-date placeholder="请选择计划完工期" v-model="model.planEndDate"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="原工单号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="originalOrderNo">
              <a-input v-model="model.originalOrderNo" placeholder="请输入原工单号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="施工号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="orderId">
              <a-input v-model="model.orderId" placeholder="请输入施工号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="20">
            <a-form-model-item label="备注" :labelCol="labelCol2" :wrapperCol="wrapperCol2" prop="remark">
              <a-textarea v-model="model.remark" rows="3" placeholder="请输入备注" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { genBillNo } from '@/utils/util'
  import moment from "dayjs";

  export default {
    name: 'PpWorkOrderForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
            orderType:"01",
            status:"10",
            sourceType:"01",
            orderQty: 0.00,
            putDate: moment(new Date()).format('YYYY-MM-DD'),
            planStartDate: moment(new Date()).format('YYYY-MM-DD')
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        labelCol2: {
          xs: { span: 24 },
          sm: { span: 2 },
        },
        wrapperCol2: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           orderType: [
              { required: true, message: '请输入工单类型!'},
           ],
           putDate: [
              { required: true, message: '请输入投放日期!'},
           ],
          materialDesc: [
            { required: true, message: '请选择产品信息!'},
          ],
        },
        url: {
          add: "/pp/workOrder/add",
          edit: "/pp/workOrder/edit",
          queryById: "/pp/workOrder/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
        genBillNo('47', this, 'orderNo');
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
      materialPopupCallback(value,row){
        this.model = Object.assign(this.model, row);
      },
    }
  }
</script>